#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution
{
public:
    int totalFruit(vector<int>& fruits)
    {
        int left = 0, right = 0, len = 0;
        int a = -1, lena = 0, lenb = 0, b = -1;
        while (right < fruits.size())
        {
            if (a == -1) a = fruits[right], lena++;
            else if (a == fruits[right]) lena++;

            if (b == -1 && a != fruits[right]) b = fruits[right], lenb++;
            else if (b == fruits[right]) lenb++;

            while (fruits[right] != a && fruits[right] != b)
            {
                if (fruits[left] == a)
                {
                    lena--;
                    if (lena == 0)
                    {
                        a = -1;
                        a = fruits[right];
                        lena++;
                    }
                }
                else
                {
                    lenb--;
                    if (lenb == 0)
                    {
                        b = -1;
                        b = fruits[right];
                        lenb++;
                    }
                }
                left++;
            }
            len = max(len, lena + lenb);
            right++;
        }
        return len;
    }
};

void test()
{
    vector<int> nums = { 3,3,3,1,2,1,1,2,3,3,4 };
    Solution s;
    cout << s.totalFruit(nums) << endl;
}

int main()
{
    test();
    return 0;
}